<?php defined('SYSPATH') OR die('No direct access allowed.');

class Email_Plugin extends WB_FormItem {
	
	protected $db_type;			// valid: int, text
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params, 'text email');
	}
	
	// TODO: validation
	public function prep_db()
	{
		return 
			valid::email($this->value) ? 
				(
					$this->db_type=='int' ?
						(int)$this->value :
						$this->value
				) :
				null;
	}
	
	public function set_value($value, $type)
	{
		if ($type=='add' || $type=='edit')
		{
			if ( ! empty($value) && ( ! valid::email($value) || ! valid::email_domain($value)) )
			{
				Kohana::instance()->user_messages->add(Kohana::lang('email_plugin.not_valid', $this->label), 'error');
				$this->value = NULL;
				return;
			}
		}
		parent::set_value($value, $type);
	}
	
	public function render_cell()
	{
		return empty($this->value) ? '&nbsp;' : html::mailto($this->value); 
	}
}

//End of Email_Plugin Library